/*
 * Copyright 2008 Max Kugland
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.splink.asbeangen.templates;

import java.util.ArrayList;
import java.util.List;

import org.splink.asbeangen.Const;
import org.splink.asbeangen.templates.as3.AS3DataClass;
import org.splink.asbeangen.templates.as3.AS3ReaderClass;
import org.splink.asbeangen.templates.as3.AS3Template;
import org.splink.asbeangen.templates.as3.AS3TemplateImplicitGetters;

public class AS3TemplateFactory implements ITemplateFactory {
	public static String TYPE = "AS3";
	private String _interfaceBasePackage;
	private ArrayList<ITemplateReaderClass> _readerClazzes;
	private ArrayList<ITemplateDataClass> _dataClazzes;
	private String _gettersType;
	private boolean _hasToXml;

	public AS3TemplateFactory(String interfaceBasePackage, String gettersType, boolean hasToXml) {
		_interfaceBasePackage = interfaceBasePackage;
		_gettersType = gettersType;
		_hasToXml = hasToXml;

		_readerClazzes = new ArrayList<ITemplateReaderClass>();
		_dataClazzes = new ArrayList<ITemplateDataClass>();
	}

	public String getType() {
		return TYPE;
	}

	public String getInterfaceBasepackage() {
		return _interfaceBasePackage;
	}

	public List<String> getCopyright() {
		List<String> copyright = new ArrayList<String>();
		copyright.add("Generated by AsBeanGen");
		copyright.add("http://www.splink.org");
		return copyright;
	}

	public IClassTemplate getClassTemplate() {
		if (_gettersType.equals(Const.GETTERS_TYPE_IMPLICIT)) {
			return new AS3TemplateImplicitGetters(AS3DataClass.DATA_CLASS_POSTFIX, AS3DataClass.READER_CLASS_POSTFIX);
		} else if (_gettersType.equals(Const.GETTERS_TYPE_EXPLICIT)) {
			return new AS3Template(AS3DataClass.DATA_CLASS_POSTFIX, AS3DataClass.READER_CLASS_POSTFIX);
		}
		return null;
	}

	public ITemplateDataClass createDataClass(String packagename, String classname, List<String> interfazes) {

		ITemplateDataClass clazz = getDataClass(classname);

		if (clazz == null) {
			clazz = new AS3DataClass(getCopyright(), packagename, classname, interfazes, _interfaceBasePackage,
					getClassTemplate(), _hasToXml);
			_dataClazzes.add(clazz);
		}

		return clazz;
	}

	public ITemplateReaderClass createReaderClass(String packagename, String classname, List<String> interfazes) {

		ITemplateReaderClass clazz = getReaderClass(classname);

		if (clazz == null) {
			clazz = new AS3ReaderClass(getCopyright(), packagename, classname, interfazes, _interfaceBasePackage,
					getClassTemplate());
			_readerClazzes.add(clazz);
		}

		return clazz;
	}

	private ITemplateDataClass getDataClass(String classname) {
		for (ITemplateDataClass dc : _dataClazzes) {
			if (dc.getID().equals(classname)) {
				return dc;
			}
		}
		return null;
	}

	private ITemplateReaderClass getReaderClass(String classname) {
		for (ITemplateReaderClass rc : _readerClazzes) {
			if (rc.getID().equals(classname)) {
				return rc;
			}
		}
		return null;
	}
}
